layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form,
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    //用于同步编辑器内容到textarea
    layedit.sync(editIndex);

    //上传缩略图
    upload.render({
        elem: '.thumbBox',
        url: '/up/lay',
        done: function(res, index, upload){
            var src = res.data.src;
            $("#banner").val(src);
            $('.thumbImg').attr('src',src);
            $('.thumbBox').css("background","#fff");
        }
    });

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());
    laydate.render({
        elem: '#release',
        type: 'datetime',
        trigger : "click",
        done : function(value, date, endDate){
            submitTime = value;
        }
    });
    form.on("radio(release)",function(data){
        if(data.elem.title == "定时发布"){
            $(".releaseDate").removeClass("layui-hide");
            $(".releaseDate #release").attr("lay-verify","required");
        }else{
            $(".releaseDate").addClass("layui-hide");
            $(".releaseDate #release").removeAttr("lay-verify");
            submitTime = time.getFullYear()+'-'+(time.getMonth()+1)+'-'+time.getDate()+' '+time.getHours()+':'+time.getMinutes()+':'+time.getSeconds();
        }
    });

    form.on("submit(addBlog)",function(data){
        var blogTypeCheckeds = [];
        $(".blogType").find("input[name='blog_type']:checked").each(function(){
            blogTypeCheckeds.push($(this).attr("type-id"));
        });
        $("#blogTypeId").val(JSON.stringify(blogTypeCheckeds));

        var content = layedit.getContent(editIndex);
        $("#content").val(content);

        var openness = $(".openness").find("input[name='openness']:checked").val();
        $("#openup").val(openness);
        var options = {
            dataType: "json",
            success: function (data) {
                if(data && data.code==0){
                    layer.msg("添加博客成功~",{icon:6});
                    setTimeout(function(){
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                    },500);
                }else{
                    layer.msg(data.msg?data.msg:"创建失败~",{icon:5});
                }

            },
            error: function (data) {
                layer.msg(data.msg?data.msg:"创建失败~");
            }
        };
        $("#listForm").ajaxSubmit(options);
    })

    //创建一个编辑器
    layedit.set({
        uploadImage: {
            url: '/up/lay' //接口url
            , type: 'post' //默认post
        }
    });
    var editIndex = layedit.build('contents',{
        tool: ["strong", "italic", "underline", "del", "|", "left", "center", "right", "|", "link", "unlink", "face", "image","code"],
        height : 535,

    });

})